package ReadWriteFieldsRelations;


import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;


public class ReadWriteFields {
  

  final Map<String, Integer> readFields = new LinkedHashMap<String, Integer>();
   final Map<String, Integer> writeFields = new LinkedHashMap<String, Integer>();

  final Set<Method> calls = new LinkedHashSet<Method>();
  
  public   Map<String, Integer> getWriteFields(){
	  return writeFields;
  }
  boolean readFieldExist(String fieldName){
	    if(!readFields.containsKey(fieldName)) 
	    	return true;
	   return false;
  }
  boolean writeFieldExist(String fieldName){
	    if(!writeFields.containsKey(fieldName)) 
	    	return true;
		return false;
}

  void read(String fieldName) {
	 if( CanUseFiledsName(fieldName))
		 	this.updateMap(readFields, fieldName, 1);
  }
  
  void read(String fieldName, int num) {
	  if( CanUseFiledsName(fieldName))
		  	this.updateMap(readFields, fieldName, num);
  }
  
  void write(String fieldName) {
	  if( CanUseFiledsName(fieldName))
		  	this.updateMap(writeFields, fieldName, 1);
  }
  
  void write(String fieldName, int num) {
	  if( CanUseFiledsName(fieldName))
		  this.updateMap(writeFields, fieldName, num);
  }
  
  private void updateMap(Map<String, Integer> map, String fieldName, int num) {
	  if( CanUseFiledsName(fieldName))
	    if(!map.containsKey(fieldName)) {
	      map.put(fieldName, num);
	    }
	    else
	    	    map.put(fieldName, map.get(fieldName) + num);
  }
  
  void call(Method method) {
    if(method == null) {
      return;
    }
    this.calls.add(method);
  }
  
  Set<Method> calls() {
    return this.calls;
  }
  
  void clearCalls() {
    calls.clear();
  }
  public static boolean CanUseFiledsName(String name) {
		if ( name.equals("statements") ||  name.equals("conditionCounters")
				||  name.equals("loops") ||  name.equals("branches") )
			return false; 
		
		return true;



  }
}
